package com.louis.iread.bean;

import android.content.Context;
import android.content.Intent;
import android.databinding.BindingAdapter;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.louis.iread.ui.NovelInfoActivity;
import com.louis.iread.R;
import com.louis.iread.utils.ConstValue;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by louis on 2016/11/10.
 */

public class SearchResultItem implements Comparable, Serializable {
    java.lang.String novelUrl;

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    String cover;
    java.lang.String novelTitle;
    java.lang.String novelLatestArticle;
    java.lang.String novelDesc;
    java.lang.String novelAuthor;
    java.lang.String novelType;
    java.lang.String novelUpdateTime;

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getNovelId() {
        return novelId;
    }

    public void setNovelId(String novelId) {
        this.novelId = novelId;
    }

    String typeId;
    String novelId;

    public String getNovelAuthor() {
        return novelAuthor;
    }

    public void setNovelAuthor(String novelAuthor) {
        this.novelAuthor = novelAuthor;
    }

    public Double getSimilarValue() {
        return similarValue;
    }

    public void setSimilarValue(String first, String second) {
        this.similarValue = cosSimilarityByString(first, second);
    }

    Double similarValue;

    public String getNovelIdInSite() {
        return novelIdInSite;
    }

    public void setNovelIdInSite(String novelIdInSite) {
        this.novelIdInSite = novelIdInSite;
    }

    private String novelIdInSite;

    public Website getWebsite() {
        return website;
    }

    public void setWebsite(Website website) {
        this.website = website;
    }

    private Website website;

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    private String siteName;

    public SearchResultItem() {

    }

    public java.lang.String getNovelUrl() {
        return website.getMainUrl().replace("{typeId}", typeId).replace("{novelId}", novelId);
    }

    private String contentsUrl;

    public String getContentsUrl() {
        return website.getContentsUrl().replace("{typeId}", typeId).replace("{novelId}", novelId);
    }

    public java.lang.String getNovelTitle() {
        return novelTitle;
    }

    public void setNovelTitle(java.lang.String novelTitle) {
        this.novelTitle = novelTitle;
    }

    public java.lang.String getNovelLatestArticle() {
        return novelLatestArticle;
    }

    public void setNovelLatestArticle(java.lang.String novelLatestArticle) {
        this.novelLatestArticle = novelLatestArticle;
    }

    public java.lang.String getNovelDesc() {
        return novelDesc;
    }

    public void setNovelDesc(java.lang.String novelDesc) {
        this.novelDesc = novelDesc;
    }


    public java.lang.String getNovelType() {
        return novelType;
    }

    public void setNovelType(java.lang.String novelType) {
        this.novelType = novelType;
    }

    public java.lang.String getNovelUpdateTime() {
        return novelUpdateTime;
    }

    public void setNovelUpdateTime(java.lang.String novelUpdateTime) {
        this.novelUpdateTime = novelUpdateTime;
    }

    @BindingAdapter("cover")
    public static void loadInternetImage(ImageView iv, String coverUrl) {
        Glide.with(iv.getContext())
                .load(coverUrl)
                .apply(new RequestOptions().placeholder(R.drawable.nocover).error(R.drawable.nocover))
                .into(iv);
    }

    /**
     * @param @param  first
     * @param @param  second
     * @param @return
     * @return Double
     * @throws
     * @Title: cosSimilarityByString
     * @Description: 得到两个字符串的相似性
     */
    private Double cosSimilarityByString(String doc1, String doc2) {
        if (doc1 != null && doc1.trim().length() > 0 && doc2 != null && doc2.trim().length() > 0) {
            Map<Integer, int[]> AlgorithmMap = new HashMap<Integer, int[]>();
            //将两个字符串中的中文字符以及出现的总数封装到，AlgorithmMap中
            for (int i = 0; i < doc1.length(); i++) {

                char d1 = doc1.charAt(i);
                if (isHanZi(d1)) {
                    int charIndex = getGB2312Id(d1);  //ch在GB2312中的位置
                    if (charIndex != -1) {
                        int[] fq = AlgorithmMap.get(charIndex);
                        if (fq != null && fq.length == 2) {
                            fq[0]++;
                        } else {
                            fq = new int[2];
                            fq[0] = 1;
                            fq[1] = 0;
                            AlgorithmMap.put(charIndex, fq);
                        }
                    }
                }
            }

            for (int i = 0; i < doc2.length(); i++) {

                char d2 = doc2.charAt(i);

                if (isHanZi(d2)) {
                    int charIndex = getGB2312Id(d2);
                    if (charIndex != -1) {
                        int[] fq = AlgorithmMap.get(charIndex);
                        if (fq != null && fq.length == 2) {
                            fq[1]++;
                        } else {
                            fq = new int[2];
                            fq[0] = 0;
                            fq[1] = 1;
                            AlgorithmMap.put(charIndex, fq);
                        }
                    }
                }
            }
            Iterator<Integer> iterator = AlgorithmMap.keySet().iterator();
            double sqdoc1 = 0;
            double sqdoc2 = 0;
            double denominator = 0;
            while (iterator.hasNext()) {
                int[] c = AlgorithmMap.get(iterator.next());
                denominator += c[0] * c[1];
                sqdoc1 += c[0] * c[0];
                sqdoc2 += c[1] * c[1];
            }
            return denominator / Math.sqrt(sqdoc1 * sqdoc2);
        } else {
            throw new NullPointerException(
                    " the Document is null or have not cahrs!!");
        }
    }

    public static boolean isHanZi(char ch) {
        // 判断是否汉字
        return (ch >= 0x4E00 && ch <= 0x9FA5);
    }

    public static short getGB2312Id(char ch) {
        try {
            byte[] buffer = Character.toString(ch).getBytes("GB2312");
            if (buffer.length != 2) {
                // 正常情况下buffer应该是两个字节，否则说明ch不属于GB2312编码，故返回'?'，此时说明不认识该字符
                return -1;
            }
            int b0 = (int) (buffer[0] & 0x0FF) - 161; // 编码从A1开始，因此减去0xA1=161
            int b1 = (int) (buffer[1] & 0x0FF) - 161; // 第一个字符和最后一个字符没有汉字，因此每个区只收16*6-2=94个汉字
            return (short) (b0 * 94 + b1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int compareTo(@NonNull Object o) {
        SearchResultItem obj = (SearchResultItem) o;
        if (o == null) {
            return -1;
        }
        return obj.getSimilarValue().compareTo(similarValue);
    }

    public void onClick(View view) {
        Context context = view.getContext();
        Intent intent = new Intent(context, NovelInfoActivity.class);

        intent.putExtra(ConstValue.SEARCH_ITEM, this);

        context.startActivity(intent);
    }
}
